********************************************************************************
********************************************************************************
******       		                             						  ******
******   STATA DATA ANALYSIS PACKAGE               						  ******
******   ===========================									  ******
******   Valuing the Benefits of Reducing Firearms        	 	  		  ******
******   Violence in the United States							          ******
******       		                             						  ******
********************************************************************************
********************************************************************************


/*
***  PURPOSE:   This Stata do-file is the master file that executes all subordinate do-files


***  OUTLINE:   PART 1: GENERAL SETTINGS
				PART 2: DOCUMENTATION AND DATA PREPARATIONS 
                PART 3: ANALYSIS
							   
***  AUTHORS:	Marc Jeuland, Duke University; marc.jeuland@duke.edu
*/

	   
********************************************************************************
******  PART 1.  GENERAL SETTINGS
********************************************************************************
	  
	 clear all
	 
******  Set dynamic file paths
	global root "C:\Users\maj8\Desktop\Working\PNAS article replication files"  		
	// [UPDATE ROOT DIRECTORY "~\PNAS article replication files" HERE]
		
		
******  Set globals for main sub-folders
	global input  	"$root\Data"
	global output 	"$root\Analysis"
	global scripts  "$root\Scripts"

******  Create additional sub-folders
	capture mkdir "$output"
	capture mkdir "$output\logs"
	
******  Stata version control - code prepared to be run with this version of Stata
	version 18.5

******  Additional settings
	set varabbrev off    // do not allow abbreviating variable names
	pause on

		   
********************************************************************************
******  PART 2.  DOCUMENTATION AND DATA PREPARATION
********************************************************************************

local packages_local 		1		// Shall all user-written packages be retrieved from the local folder $scripts/packages (and installed, if necessary)? yes/no (1/0)

******  Install packages locally
	if (`packages_local' == 1) { 
		capture makedir "$scripts\packages"
		sysdir set PLUS "$scripts\packages"
		
		*** Install packages from SSC
			local ssc_packages "doubleb"
			
			// avoid re-installing if already present
			if !missing("`ssc_packages'") {
				foreach pkg of local ssc_packages {
					capture which `pkg'
					if _rc == 111 {                 
						dis "Installing `pkg'"
						quietly ssc install `pkg', replace
					}
				}
			}
			quietly ssc install estout, replace
	}
	
************************* Variable labeling ************************************

use "$input\gunviolence_county_merged.dta", clear

label variable rnd_01 "Controls randomization at GS1"
label variable rnd_02 "Controls randomization at SA1 & GVS3"
label variable rnd_03 "Controls randomization at GVS2"
label variable rnd_08 "Type of reduction policy (0=Gun violence, 1=All crime)"
label variable rnd_09 "Effectiveness level of reduction policy (0=20%, 1=40%)"
label variable rnd_10 "Price level of reduction policy (0=$75, 1=$200, 2=$500, 3=$1200)"
label variable gvs1a "[The economy and jobs] Do you believe each of the following is a major problem, minor problem, or not a problem at all in the United States?"
label variable gvs1b "[Inflation] Do you believe each of the following is a major problem, minor problem, or not a problem at all in the United States?"
label variable gvs1c "[Gun violence] Do you believe each of the following is a major problem, minor problem, or not a problem at all in the United States?"
label variable gvs1d "[Illegal immigration] Do you believe each of the following is a major problem, minor problem, or not a problem at all in the United States?"
label variable gs1 "Should gun laws in the United States be made more strict, left as they are, or made less strict?"
label variable gs2a "[A nationwide ban on the sale of AR-15 rifles and similar semiautomatic weapons] Do you favor, oppose, or neither favor nor oppose each of the following policies?"
label variable gs2b "[A federal law requiring background checks on all potential gun buyers, including private sales and gun shows] Do you favor, oppose, or neither favor nor oppose each of the following policies?"
label variable gs2c "[A federal law preventing mentally ill people from purchasing guns] Do you favor, oppose, or neither favor nor oppose each of the following policies?"
label variable gs2d "[Allowing courts to temporarily prevent people who are considered a danger to themselves or others, but have not been convicted of a crime, from owning a gun] Do you favor, oppose, or neither favor nor oppose each of the following policies?"
label variable gs2e "[Making 21 the minimum legal age to buy any gun nationwide] Do you favor, oppose, or neither favor nor oppose each of the following policies?"
label variable gs2f "[A law in your state allowing people to carry guns in public without a permit] Do you favor, oppose, or neither favor nor oppose each of the following policies?"
label variable gs2g "[A federal law that bans those convicted of domestic violence from purchasing a gun] Do you favor, oppose, or neither favor nor oppose each of the following policies?"
label variable gs2h "[A federal law requiring expanded background checks for gun buyers between 18 and 21 years of age] Do you favor, oppose, or neither favor nor oppose each of the following policies?"
label variable gce1a "[Mass shootings] If it were harder for people to legally obtain guns in the United States, do you think there would be fewer or more of each of the following or wouldn't it make much of a difference?"
label variable gce1b "[Suicides] If it were harder for people to legally obtain guns in the United States, do you think there would be fewer or more of each of the following or wouldn't it make much of a difference?"
label variable gce1c "[Murders] If it were harder for people to legally obtain guns in the United States, do you think there would be fewer or more of each of the following or wouldn't it make much of a difference?"
label variable gce1d "[Robberies] If it were harder for people to legally obtain guns in the United States, do you think there would be fewer or more of each of the following or wouldn't it make much of a difference?"
label variable sa1a "[Reducing all types of gun violence] How important to you is each of the following?"
label variable sa1b "[Preventing mass shootings] How important to you is each of the following?"
label variable sa1c "[Protecting the right to own guns] How important to you is each of the following?"
label variable sa1d "[Making sure people can own guns for personal protection] How important to you is each of the following?"
label variable gvs2a "[The United States] In the past 5 years, do you believe gun violence has [increased, decreased/decreased, increased,] or remained the same in:"
label variable gvs2b "[Your state] In the past 5 years, do you believe gun violence has [increased, decreased/decreased, increased,] or remained the same in:"
label variable gvs2c "[Your community] In the past 5 years, do you believe gun violence has [increased, decreased/decreased, increased,] or remained the same in:"
label variable gvs3 "How likely do you think it is that you personally might be the victim of gun violence within the next five years?"
label variable pe1_1 "[Yes, I have personally experienced it] Have you, someone in your family, or a close friend experienced gun violence in the last five years? For example, by being threatened with a gun or being the victim of a shooting."
label variable pe1_2 "[Yes, someone in my family has experienced it] Have you, someone in your family, or a close friend experienced gun violence in the last five years? For example, by being threatened with a gun or being the victim of a shooting."
label variable pe1_3 "[Yes, a close friend has experienced it] Have you, someone in your family, or a close friend experienced gun violence in the last five years? For example, by being threatened with a gun or being the victim of a shooting."
label variable pe1_4 "[No] Have you, someone in your family, or a close friend experienced gun violence in the last five years? For example, by being threatened with a gun or being the victim of a shooting."
label variable pe1_dk "[DON'T KNOW] Have you, someone in your family, or a close friend experienced gun violence in the last five years? For example, by being threatened with a gun or being the victim of a shooting."
label variable pe1_skp "[SKIPPED ON WEB] Have you, someone in your family, or a close friend experienced gun violence in the last five years? For example, by being threatened with a gun or being the victim of a shooting."
label variable pe1_ref "[REFUSED] Have you, someone in your family, or a close friend experienced gun violence in the last five years? For example, by being threatened with a gun or being the victim of a shooting."
label variable pe2 "Do you happen to have in your home or garage any guns? Please include any handguns, pistols, revolvers, shotguns, or rifles."
label variable harris_insert1 "Text insert for HARRIS_DISPLAY2/ HARRIS2/ HARRIS/ HARRIS6A/HARRIS11/ HARRIS_DISPLAY3 gun violence/ overall crime" 
label variable harris_insert2 "Text insert for percentages at HARRIS_DISPLAY2 & HARRIS2:  20%/ 40%"
label variable harris_insert3 "Text insert for cost amounts at HARRIS2/ HARRIS5A/ HARRIS5B: $75/ $200 / $500 / $1200"
label variable harris_insert4 "Text insert for double cost amounts at HARRIS5B  $150/ $400 / $1,000 / $2,400"
label variable harris_insert5 "DUMMY QUESTION USED TO INSERT TEXT"
label variable harris_display1_insert "Text Insert for HARRIS_DISPLAY1"
label variable harris_display1_totaltime "Time spent reading screen part 1"
label variable harris1 "We want to make sure you understand what the policies being voted on would do. Please indicate, what would be the extent of the reduction in crime?"
label variable harris1_totaltime "Time to answer comprehension question"
label variable harris_correct1_totaltime "Time on screen if correct comprehension question"
label variable harris_incorrect1_insert "Text Insert for HARRIS_INCORRECT1"
label variable harris_incorrect1_totaltime "Time on screen if incorrect comprehension question"
label variable harris_display2_totaltime "Time spent reading script part 2"
label variable harris2 "...At this cost, would you vote in favor of this proposal that would impose this fee and implement the programs, knowing that [gun violence/overall crime] would be reduced by [20%/40%] as a result?"
label variable harris2_totaltime "Time to answer WTP question"
label variable harris3 "How certain are you about your response?"
label variable harris4a_1 "[[Gun violence/Crime] is not a serious problem where I live and does not affect my household] Why did you say you would not vote for (or were uncertain about) this proposal?"
label variable harris4a_2 "[My household could not afford this new fee] Why did you say you would not vote for (or were uncertain about) this proposal?"
label variable harris4a_3 "[My household could afford this fee, but I believe it is too high given the [20%/40%] reduction in [gun violence/crime]] Why did you say you would not vote for (or were uncertain about) this proposal?"
label variable harris4a_4 "[I do not believe that these policies would actually work as described] Why did you say you would not vote for (or were uncertain about) this proposal?"
label variable harris4a_5 "[I am concerned about other impacts of these policies, please describe:] Why did you say you would not vote for (or were uncertain about) this proposal?"
label variable harris4a_5_oe "Explanation for harris4a_5"
label variable harris4a_6 "[Though I support the policy and believe it is worth this amount, I am opposed to fees like this] Why did you say you would not vote for (or were uncertain about) this proposal?"
label variable harris4a_7 "[Other, specify:] Why did you say you would not vote for (or were uncertain about) this proposal?"
label variable harris4a_7_oe "Explanation for harris4a_7"
label variable harris4a_dk "[DON'T KNOW] Why did you say you would not vote for (or were uncertain about) this proposal?"
label variable harris4a_skp "[SKIPPED ON WEB] Why did you say you would not vote for (or were uncertain about) this proposal?"
label variable harris4a_ref "[REFUSED] Why did you say you would not vote for (or were uncertain about) this proposal?"
label variable harris4b_1 "[Reduced risk of death of you or someone in your household dying from [gun violence/crime].] You said you would vote in favor of this proposal. Can you tell us which of the following benefits are most important to you?"
label variable harris4b_2 "[Improved peace of mind to you and other members of your household.] You said you would vote in favor of this proposal. Can you tell us which of the following benefits are most important to you?"
label variable harris4b_3 "[New comfort or ability to work, recreate, or live in other parts of your area that would now be safer.] You said you would vote in favor of this proposal. Can you tell us which of the following benefits are most important to you?"
label variable harris4b_4 "[Reduced risk of other people (outside your household) being hurt or killed.] You said you would vote in favor of this proposal. Can you tell us which of the following benefits are most important to you?"
label variable harris4b_5 "[Improved peace of mind to other people (outside your household).] You said you would vote in favor of this proposal. Can you tell us which of the following benefits are most important to you?"
label variable harris4b_dk "[DON'T KNOW] You said you would vote in favor of this proposal. Can you tell us which of the following benefits are most important to you?"
label variable harris4b_skp "[SKIPPED ON WEB] You said you would vote in favor of this proposal. Can you tell us which of the following benefits are most important to you?"
label variable harris4b_ref "[REFUSED] You said you would vote in favor of this proposal. Can you tell us which of the following benefits are most important to you?"
label variable harris5a "...At this lower cost, would you vote in favor of the referendum that would impose this fee and implement the programs?"
label variable harris5b "...At this higher cost, would you vote in favor of the referendum that would impose this fee and implement the programs?"
label variable harris6a_1 "[Reduced risk of death of you or someone in your household dying from [gun violence/crime].] You said you would now vote in favor of this proposal.  Can you tell us which of the following benefits are most important to you?"
label variable harris6a_2 "[Improved peace of mind to you and other members of your household.] You said you would now vote in favor of this proposal.  Can you tell us which of the following benefits are most important to you?"
label variable harris6a_3 "New comfort or ability to work, recreate, or live in other parts of your area that would now be safer.] You said you would now vote in favor of this proposal.  Can you tell us which of the following benefits are most important to you?"
label variable harris6a_4 "[Reduced risk of other people (outside your household) being hurt or killed.] You said you would now vote in favor of this proposal.  Can you tell us which of the following benefits are most important to you?"
label variable harris6a_5 "[Improved peace of mind to other people (outside your household).] You said you would now vote in favor of this proposal.  Can you tell us which of the following benefits are most important to you?"
label variable harris6a_dk "[DON'T KNOW] You said you would now vote in favor of this proposal.  Can you tell us which of the following benefits are most important to you?"
label variable harris6a_skp "[SKIPPED ON WEB] You said you would now vote in favor of this proposal.  Can you tell us which of the following benefits are most important to you?"
label variable harris6a_ref "[REFUSED] You said you would now vote in favor of this proposal.  Can you tell us which of the following benefits are most important to you?"
label variable harris6b "...Now imagine that there were no fees needed to fund these policies, because they would be paid for by another source at no cost to you. In that case, would you vote in favor of the referendum that would implement the programs?"
label variable harris7 "...Do you spend money on private security or alarm systems to protect yourself or your property (for example, guard dogs, pepper spray, or self-defense weapons)?"
label variable harris7b "[TEXTBOX] If yes, what is the approximate annual cost of this security in dollars:"
label variable harris8 "Do you or people in your household avoid going to certain areas in your community, either all the time or at specific times, to reduce gun-violence related risks?"
label variable harris9_1 "[Regular errands for food or other household needs] What types of activities do you feel you must avoid due to these safety concerns?"
label variable harris9_2 "[Entertainment or social activities] What types of activities do you feel you must avoid due to these safety concerns?"
label variable harris9_3 "[Recreational activities (walking in the neighborhood, sports, going to the gym, etc.)] What types of activities do you feel you must avoid due to these safety concerns?"
label variable harris9_4 "[Visiting friends or family] What types of activities do you feel you must avoid due to these safety concerns?"
label variable harris9_5 "[Other, please indicate what:] What types of activities do you feel you must avoid due to these safety concerns?"
label variable harris9_5_oe "Indication for harris9_5"
label variable harris9_dk "[DON'T KNOW] What types of activities do you feel you must avoid due to these safety concerns?"
label variable harris9_skp "[SKIPPED ON WEB] What types of activities do you feel you must avoid due to these safety concerns?"
label variable harris9_ref "[REFUSED] What types of activities do you feel you must avoid due to these safety concerns?"
label variable harris10 "Think about the various reasons you chose to live where you currently live. Did you move in the past ten years?"
label variable harris11 "How important was the threat of [gun violence/crime] in influencing your decision to stay where you currently live (if you did not move), or to where you moved (if you did)?"
label variable harris12a "[The US federal government] How effective do you think government is in general?"
label variable harris12b "[Your state government] How effective do you think government is in general?"
label variable harris13 "Which of the following best describes your personal views on government taxes overall, including all local, state and federal taxes you face?"
label variable harris14 "Do you think the tax burden in your state and community falls more on the very wealthy, more on the very poor, or no difference?"
label variable harris15 "How serious is [gun violence/crime] in your community and the surrounding areas that you regularly visit?"
label variable harris16 "What is your overall view of the role that police play in your local community?"
label variable harris17_1 "[Bad people] What do you think is most responsible for causing [gun violence/crime], in general?"
label variable harris17_2 "[Social distress — poverty, unemployment, lack of opportunity/education] What do you think is most responsible for causing [gun violence/crime], in general?"
label variable harris17_3 "[Courts too lenient, punishments not severe enough] What do you think is most responsible for causing [gun violence/crime], in general?"
label variable harris17_4 "[Not enough police] What do you think is most responsible for causing [gun violence/crime], in general?"
label variable harris17_5 "[Availability of guns/weapons] What do you think is most responsible for causing [gun violence/crime], in general?"
label variable harris17_6 "[Decline of social values and norms] What do you think is most responsible for causing [gun violence/crime], in general?"
label variable harris17_7 "[Drugs] What do you think is most responsible for causing [gun violence/crime], in general?"
label variable harris17_8 "[Violence in the media] What do you think is most responsible for causing [gun violence/crime], in general?"
label variable harris17_9 "[Other, please specify] What do you think is most responsible for causing [gun violence/crime], in general?"
label variable harris17_9_oe "Specify for harris17_9"
label variable harris17_dk "[Not sure / don't know] What do you think is most responsible for causing [gun violence/crime], in general?"
label variable harris17_skp "[SKIPPED ON WEB] What do you think is most responsible for causing [gun violence/crime], in general?"
label variable harris17_ref "[REFUSED] What do you think is most responsible for causing [gun violence/crime], in general?"
label variable harris18 "Have you ever heard gunshots that scare you in your neighborhood?"
label variable partyid7 "DATA-ONLY: Computed 7-level Party ID"
label variable partyid5 "DATA-ONLY: Computed 5-level Party ID"
label variable dm5 "How would you describe the community you live in now?"
label variable ideo "DATA-ONLY: Computed 5-level political ideology"
label variable relig "What is your present religion, if any?"
label variable relig_oe "Other religion, specification"
label variable born "Would you describe yourself as a 'born-again' or evangelical Christian, or not?"
label variable startdt_harris "UCHI/CrimeLabs: Guns/Overall Crime Omnibus Section start date/time"
label variable enddt_harris "UCHI/CrimeLabs: Guns/Overall Crime Omnibus Section end date/time"
label variable duration_harris "UCHI/CrimeLabs: Guns/Overall Crime Omnibus Section duration"
label variable surv_mode "Survey interview mode (online or phone)"
label variable surv_lang "Survey Interview Language"
label variable device "Device used"
label variable gender "Respondent gender"
label variable age "Age - Years"
label variable age4 "Age - 4 Categories"
label variable age7 "Age - 7 Categories"
label variable racethnicity "Combined Race/Ethnicity"
label variable educ5 "5-level education"
label variable marital "Marital Status"
label variable employ "Current Employment Status"
label variable income "Household Income"
label variable income4 "4-level Household Income"
label variable income9 "9-level Household Income"
label variable state "State"
label variable region4 "4-level region"
label variable region9 "9-level region"
label variable metro "Metropolitan area flag"
label variable internet "HH internet access via dial-up, DSL, or cable broadband at home"
label variable housing "Home Ownership"
label variable home_type "Type of building of panelists' residence"
label variable phoneservice "Telephone service for the household"
label variable hhsize "Household size"
label variable hh01 "Number of household members ages 0-1"
label variable hh25 "Number of household members ages 2-5" 
label variable hh612 "Number of household members ages 6-12"
label variable hh1317 "Number of household members ages 13-17"
label variable hh18ov "Number of household members ages 18 and over"

************** Variable cleaning ***********************************************

replace avg_gun_hom_rate_cdc = "." if avg_gun_hom_rate_cdc == "NA" | avg_gun_hom_rate_cdc == "Inf"
replace avg_gun_hom_rate_calculated = "." if avg_gun_hom_rate_calculated == "NA" | avg_gun_hom_rate_calculated == "Inf"
destring avg_gun_hom_rate_cdc, force replace
destring avg_gun_hom_rate_calculated, force replace

forvalues i= 1(1)3 {
sum avg_gun_hom_rate_cdc avg_gun_hom_rate_calculated if dm5 == `i'
count if avg_gun_hom_rate_cdc == . & dm5 == `i'
count if avg_gun_hom_rate_calculated == . & dm5 == `i'
	}
*Calculated gun homicide data missing for 126/672 urban obs (19%), 173/501 suburban obs (35%), 189/200 rural obs (95%)

************** Variable creation ***********************************************

gen price1 = .
replace price1 = 75 if rnd_10 == 0
replace price1 = 200 if rnd_10 == 1
replace price1 = 500 if rnd_10 == 2
replace price1 = 1200 if rnd_10 == 3
tab price1, miss

rename harris2 yesno1
replace yesno1 = . if yesno1 == 6 | yesno1 == 9
tab yesno1
tab yesno1 price1, miss

gen yesno1b = 0 if yesno1 == 1 | yesno1 == 77 | yesno1 == 98
replace yesno1b = 1 if yesno1 == 2

destring harris3, replace
rename harris3 certainwtp
tab certainwtp

replace yesno1b = 0 if yesno1 == 2 & certainwtp == 4
tab yesno1 yesno1b
tab yesno1b price1, miss

tab yesno1b harris5a
tab yesno1b harris5b

gen price2 = .
replace price2 = 150 if rnd_10 == 0 & yesno1 == 2 & certainwtp !=4
replace price2 = 400 if rnd_10 == 1 & yesno1 == 2 & certainwtp !=4
replace price2 = 1000 if rnd_10 == 2 & yesno1 == 2 & certainwtp !=4
replace price2 = 2400 if rnd_10 == 3 & yesno1 == 2 & certainwtp !=4
replace price2 = 37.5 if rnd_10 == 0 & (yesno1 == 1 | yesno1 == 77 | yesno1 == 98 | (yesno1 == 2 & certainwtp ==4))
replace price2 = 100 if rnd_10 == 1 & (yesno1 == 1 | yesno1 == 77 | yesno1 == 98 | (yesno1 == 2 & certainwtp ==4))
replace price2 = 250 if rnd_10 == 2 & (yesno1 == 1 | yesno1 == 77 | yesno1 == 98 | (yesno1 == 2 & certainwtp ==4))
replace price2 = 600 if rnd_10 == 3 & (yesno1 == 1 | yesno1 == 77 | yesno1 == 98 | (yesno1 == 2 & certainwtp ==4))

destring harris5a, replace
destring harris5b, replace
gen yesno2 = harris5a
replace yesno2 = harris5b if yesno2 == .
tab yesno2 price2

gen yesno2b = 0 if yesno2 == 1 | yesno2 == 77 | yesno2 == 98
replace yesno2b = 1 if yesno2 == 2
tab yesno2 yesno2b
tab yesno2b price2, miss

gen eff40 = 1 if rnd_09 == 1
replace eff40 = 0 if rnd_09 == 0

destring rnd_08, force replace

gen av = 1 if rnd_08 == 1
replace av = 0 if rnd_08 == 0

***Create demographic and ideology controls for analysis

tab hhsize
gen hhsize1 = (hhsize == 1)
gen hhsize2 = (hhsize == 2)
gen hhsize3 = (hhsize >= 3 & hhsize <5)
gen hhsize4 = (hhsize >= 5)

rename age4 age4cats

gen age1 = (age < 35)
gen age2 = (age >= 35 & age < 50)
gen age3 = (age >= 50 & age < 65)
gen age4 = (age >= 65)

gen ownhome = (housing == 1)
tab ownhome housing

gen phone = (surv_mode == 1)
tab phone surv_mode

gen male = (gender == 1)
tab male gender

gen children = (hh01 >= 1 | hh25 >= 1 | hh612 >= 1 | hh1317 >= 1)
gen teenchildren = (hh1317 >= 1)
gen youngchildren = (hh01 >= 1 | hh25 >= 1 | hh612 >= 1)
egen num_children = rowtotal(hh01 hh25 hh612 hh1317)
gen numteens = hh1317
egen num_youngchildren = rowtotal(hh01 hh25 hh612)
gen children1 = (num_children == 1)
gen children2 = (num_children == 2)
gen children3plus = (num_children >= 3)

gen urban = (dm5 == 1)
gen suburban = (dm5 == 2)
gen rural = (dm5 == 3)

gen northeast = (region4 == 1)
gen midwest = (region4 == 2)
gen south = (region4 == 3)
gen west = (region4 == 4)

gen conservative = (ideo == 4 | ideo == 5)
gen moderate = (ideo == 3)
gen liberal = (ideo == 1 | ideo == 2)
gen ideo_missing = (ideo == -1)
tab conservative ideo
tab liberal ideo

gen incomeg1 = (income4 == 1)
gen incomeg2 = (income4 == 2)
gen incomeg3 = (income4 == 3)
gen incomeg4 = (income4 == 4)
sum incomeg2 incomeg3 incomeg4
tab income4 incomeg2
tab income4 incomeg3
tab income4 incomeg4

gen college = (educ5 == 4 | educ5 == 5)
tab college educ5

gen black = (racethnicity == 2)
gen hispanic = (racethnicity == 4)
gen otherrace = (black == 0 & hispanic == 0)
tab racethnicity black
tab racethnicity hispanic

gen antitax = (harris13 == 1 | harris13 == 4)
gen antitaxmissing = (harris13 == 77 | harris13 == 98)
tab antitax harris13

gen gvserious = (harris15 == 1 | harris15 == 2)
gen gvnotserious = (harris15 == 3)
gen gvseriousmissing = (harris15 == 77 | harris15 == 98)

gen likelygv = (gvs3 == 1 | gvs3 == 2 | gvs3 == 3)
gen likelygvmissing = (gvs3 == 98 | gvs3 == 77)

gen locatetoavoid = (harris11 == 1 | harris11 == 2 | harris11 == 3)
gen locatetoavoidmissing = (harris11 == 77 | harris11 == 98)

gen massshooteff = (gce1a == 1 | gce1a == 2)
gen massshootunchanged = (gce1a == 3)
gen massshootineff = (gce1a == 4 | gce1a == 5)
gen massshootmissing = (gce1a == 98 | gce1a == 77)

gen suicideeff = (gce1b == 1 | gce1b == 2)
gen suicideunchanged = (gce1b == 3)
gen suicideineff = (gce1b == 4 | gce1b == 5)
gen suicidemissing = (gce1b == 77 | gce1b == 98)

gen murdereff = (gce1c == 1 | gce1c == 2)
gen murderunchanged = (gce1c == 3)
gen murderineff = (gce1d == 4 | gce1c == 5)
gen murdermissing = (gce1c == 77 | gce1c == 98)

gen robberyeff = (gce1d == 1 | gce1d == 2)
gen robberyunchanged = (gce1d == 3)
gen robberyineff = (gce1d == 4 | gce1d == 5)
gen robberymissing = (gce1d == 77 | gce1d == 98)

gen fedgoveff = (harris12a == 1 | harris12a == 2)
gen fedgovunsure = (harris12a == 3)
gen fedgovineff = (harris12a == 4 | harris12a == 5)
gen fedgovmissing = (harris12a == 77 | harris12a == 98)

gen stategoveff = (harris12b == 1 | harris12b == 2)
gen stategovunsure = (harris12b == 3)
gen stategovineff = (harris12b == 4 | harris12b == 5)
gen stategovmissing = (harris12b == 77 | harris12b == 98)

gen gunowner = (pe2 == 1)
gen gunownermissing = (pe2 == 77 | pe2 == 98)

gen massshooturgent = 1 if sa1a == 1 | sa1a == 2
replace massshooturgent = 0 if sa1a ~= 1 & sa1a ~= 2
gen suicideurgent = 1 if sa1b == 1 | sa1b == 2
replace suicideurgent = 0 if sa1b ~= 1 & sa1b ~= 2
gen murderurgent = 1 if sa1c == 1 | sa1c == 2
replace murderurgent = 0 if sa1c ~= 1 & sa1c ~= 2
gen robberyurgent = 1 if sa1d == 1 | sa1d == 2
replace robberyurgent = 0 if sa1d ~= 1 & sa1d ~= 2
tab massshooturgent sa1a
tab suicideurgent sa1b
tab murderurgent sa1c
tab robberyurgent sa1d

***Summary stats

log using "$output\logs\summarystats_weighted.log", replace

sum yesno1b price1 eff40 phone male age2 age3 age4 hhsize2 hhsize3 hhsize4 ownhome incomeg2 incomeg3 incomeg4 college black hispanic northeast midwest south urban suburban antitax antitaxmissing conservative liberal ideo_missing gvnotserious gvseriousmissing likelygv likelygvmissing locatetoavoid gunowner gunownermissing

sum yesno1b price1 eff40 phone male age2 age3 age4 hhsize2 hhsize3 hhsize4 ownhome incomeg2 incomeg3 incomeg4 college black hispanic northeast midwest south urban suburban antitax antitaxmissing conservative liberal ideo_missing gvnotserious gvseriousmissing likelygv likelygvmissing locatetoavoid gunowner gunownermissing [weight=weight1]

log close

save "$input\gunviolence_county_merged_temp.dta", replace

************** WTP analysis ****************************************************

log using "$output\logs\wtp_weighted_jul24.log", replace

drop if av == 1
for any 1 2: gen lpriceX = ln(priceX)

***Specification 1a: Simple first price and effectiveness analysis - linear
 
probit yesno1b price1 eff40 [pweight=weight1], robust
matrix b=get(_b) 
matrix xbeta=b[.,2...]   
gen betap=_b[price1] 
matrix score y=xbeta
gen py=normprob(y)
gen wtp= (py/-betap)  
sum py wtp 
drop betap y wtp py
matrix drop _all

***Specification 1b: Simple double bounded analysis w/effectiveness - log linear

doubleb lprice1 lprice2 yesno1b yesno2b eff40 [pweight=weight1]

matrix dbb=r(table)
/*Low effectiveness, gun violence*/
nlcom (WTP:(exp(dbb[1,2])*exp(0.5*(dbb[1,3]^2)))), noheader 
/*High effectiveness, gun violence*/
nlcom (WTP:(exp(dbb[1,2]+dbb[1,1])*exp(0.5*(dbb[1,3]^2)))), noheader 

***NOTE: These estimates are not bias-corrected; use results of bootstrapping just
*below to apply the bias correction. 
*Specifically, low effectiveness mean will be 780.7-36.6=744.1; and
*high effectiveness mean will be 1026.8-69.4=957.4

**** bootstrapping Model 1b - Low effectiveness, gun violence
*Define Program*
capture program drop WTPdb_boot1
program define WTPdb_boot1, rclass
version 8

quietly doubleb lprice1 lprice2 yesno1b yesno2b eff40 [pweight=weight1]
matrix dbb=r(table)
nlcom (WTP:(exp(dbb[1,2])*exp(0.5*(dbb[1,3]^2)))), noheader
matrix wtpmat = r(table)  
gen wtp = wtpmat[1,1]
scalar WTPNAME_s = wtp
return scalar WTPNAME=WTPNAME_s

drop wtp 

matrix drop _all
end

*Bootstrap standard errors*
set seed 1 
bootstrap "WTPdb_boot1" WTPNAME=r(WTPNAME), rep(5000) saving(WTPNAME) replace every(1) level(50)

**** bootstrapping Model 1b - High effectiveness, gun violence
*Define Program*
capture program drop WTPdb_boot1
program define WTPdb_boot1, rclass
version 8

quietly doubleb lprice1 lprice2 yesno1b yesno2b eff40 [pweight=weight1]
matrix dbb=r(table)
nlcom (WTP:(exp(dbb[1,2]+dbb[1,1])*exp(0.5*(dbb[1,3]^2)))), noheader 
matrix wtpmat = r(table)  
gen wtp = wtpmat[1,1]
scalar WTPNAME_s = wtp
return scalar WTPNAME=WTPNAME_s

drop wtp 

matrix drop _all
end

*Bootstrap standard errors*
set seed 1 
bootstrap "WTPdb_boot1" WTPNAME=r(WTPNAME), rep(5000) saving(WTPNAME) replace every(1) level(50)


***Specification 2a: First price analysis w/dem and SES controls

probit yesno1b price1 eff40 phone male age2 age3 age4 hhsize2 hhsize3 hhsize4 ownhome incomeg2 incomeg3 incomeg4 college black hispanic northeast midwest south urban suburban [pweight=weight1], robust
matrix b=get(_b) 
matrix xbeta=b[.,2...]   
gen betap=_b[price1] 
matrix score y=xbeta
gen py=normprob(y)
gen wtp= (py/-betap)  
sum py wtp 
drop betap y wtp py
matrix drop _all

***Specification 2b: Double bounded analysis w/effectiveness + dem and SES controls

*Calculate means of control variables HERE*

global controls avg_gun_hom_rate_calculated avg_viol_rate avg_prop_rate phone male age2 age3 age4 hhsize2 hhsize3 hhsize4 ownhome incomeg2 incomeg3 incomeg4 college black hispanic northeast midwest south urban suburban antitax antitaxmissing conservative liberal ideo_missing gvnotserious gvseriousmissing likelygv likelygvmissing locatetoavoid locatetoavoidmissing gunowner gunownermissing

foreach x of varlist $controls {
	egen `x'm = mean(`x')
	}

doubleb lprice1 lprice2 yesno1b yesno2b eff40 phone male age2 age3 age4 hhsize2 hhsize3 hhsize4 ownhome incomeg2 incomeg3 incomeg4 college black hispanic northeast midwest south urban suburban [pweight=weight1]

matrix dbb=r(table)
*Mean
/*Low effectiveness, gun violence*/
nlcom (WTP:(exp(dbb[1,22]+dbb[1,2]*phonem+dbb[1,3]*malem+dbb[1,4]*age2m+dbb[1,5]*age3m+dbb[1,6]*age4m+dbb[1,7]*hhsize2m+dbb[1,8]*hhsize3m+dbb[1,9]*hhsize4m+dbb[1,10]*ownhomem+dbb[1,11]*incomeg2m+dbb[1,12]*incomeg3m+dbb[1,13]*incomeg4m+dbb[1,14]*collegem+dbb[1,15]*blackm+dbb[1,16]*hispanicm+dbb[1,17]*northeastm+dbb[1,18]*midwestm+dbb[1,19]*southm+dbb[1,20]*urbanm+dbb[1,21]*suburbanm)*exp(0.5*(dbb[1,23]^2)))), noheader 
/*High effectiveness, gun violence*/
nlcom (WTP:(exp(dbb[1,22]+dbb[1,1]+dbb[1,2]*phonem+dbb[1,3]*malem+dbb[1,4]*age2m+dbb[1,5]*age3m+dbb[1,6]*age4m+dbb[1,7]*hhsize2m+dbb[1,8]*hhsize3m+dbb[1,9]*hhsize4m+dbb[1,10]*ownhomem+dbb[1,11]*incomeg2m+dbb[1,12]*incomeg3m+dbb[1,13]*incomeg4m+dbb[1,14]*collegem+dbb[1,15]*blackm+dbb[1,16]*hispanicm+dbb[1,17]*northeastm+dbb[1,18]*midwestm+dbb[1,19]*southm+dbb[1,20]*urbanm+dbb[1,21]*suburbanm)*exp(0.5*(dbb[1,23]^2)))), noheader 

***NOTE: These estimates are not bias-corrected; use results of bootstrapping just
*below to apply the bias correction. 
*Specifically, low effectiveness mean will be 685.7-(-58.1)=743.8; and
*high effectiveness mean will be 836.1-(-54.9)=891.0

**** Bootstrapping Specification 2b - Low effectiveness, gun violence
*Define Program*
capture program drop WTPdb_boot1
program define WTPdb_boot1, rclass
version 8

quietly doubleb lprice1 lprice2 yesno1b yesno2b eff40 phone male age2 age3 age4 hhsize2 hhsize3 hhsize4 ownhome incomeg2 incomeg3 incomeg4 college black hispanic northeast midwest south urban suburban [pweight=weight1]
matrix dbb=r(table)
nlcom (WTP:(exp(dbb[1,22]+dbb[1,2]*phonem+dbb[1,3]*malem+dbb[1,4]*age2m+dbb[1,5]*age3m+dbb[1,6]*age4m+dbb[1,7]*hhsize2m+dbb[1,8]*hhsize3m+dbb[1,9]*hhsize4m+dbb[1,10]*ownhomem+dbb[1,11]*incomeg2m+dbb[1,12]*incomeg3m+dbb[1,13]*incomeg4m+dbb[1,14]*collegem+dbb[1,15]*blackm+dbb[1,16]*hispanicm+dbb[1,17]*northeastm+dbb[1,18]*midwestm+dbb[1,19]*southm+dbb[1,20]*urbanm+dbb[1,21]*suburbanm)*exp(0.5*(dbb[1,23]^2)))), noheader 
matrix wtpmat = r(table)  
gen wtp = wtpmat[1,1]
scalar WTPNAME_s = wtp
return scalar WTPNAME=WTPNAME_s

drop wtp 

matrix drop _all
end

*Bootstrap standard errors*
set seed 1 
bootstrap "WTPdb_boot1" WTPNAME=r(WTPNAME), rep(5000) saving(WTPNAME) replace every(1) level(50)

*Mean
**** Bootstrapping Specification 2b - High effectiveness, gun violence
*Define Program*
capture program drop WTPdb_boot1
program define WTPdb_boot1, rclass
version 8

quietly doubleb lprice1 lprice2 yesno1b yesno2b eff40 phone male age2 age3 age4 hhsize2 hhsize3 hhsize4 ownhome incomeg2 incomeg3 incomeg4 college black hispanic northeast midwest south urban suburban [pweight=weight1]
matrix dbb=r(table)
nlcom (WTP:(exp(dbb[1,22]+dbb[1,1]+dbb[1,2]*phonem+dbb[1,3]*malem+dbb[1,4]*age2m+dbb[1,5]*age3m+dbb[1,6]*age4m+dbb[1,7]*hhsize2m+dbb[1,8]*hhsize3m+dbb[1,9]*hhsize4m+dbb[1,10]*ownhomem+dbb[1,11]*incomeg2m+dbb[1,12]*incomeg3m+dbb[1,13]*incomeg4m+dbb[1,14]*collegem+dbb[1,15]*blackm+dbb[1,16]*hispanicm+dbb[1,17]*northeastm+dbb[1,18]*midwestm+dbb[1,19]*southm+dbb[1,20]*urbanm+dbb[1,21]*suburbanm)*exp(0.5*(dbb[1,23]^2)))), noheader  
matrix wtpmat = r(table)  
gen wtp = wtpmat[1,1]
scalar WTPNAME_s = wtp
return scalar WTPNAME=WTPNAME_s

drop wtp 

matrix drop _all
end

*Bootstrap standard errors*
set seed 1 
bootstrap "WTPdb_boot1" WTPNAME=r(WTPNAME), rep(5000) saving(WTPNAME) replace every(1) level(50)


***Specification 3a: First price analysis w/added controls

probit yesno1b price1 eff40 phone male age2 age3 age4 hhsize2 hhsize3 hhsize4 ownhome incomeg2 incomeg3 incomeg4 college black hispanic northeast midwest south urban suburban antitax antitaxmissing conservative liberal ideo_missing gvnotserious gvseriousmissing likelygv likelygvmissing locatetoavoid gunowner gunownermissing [pweight=weight1], robust
matrix b=get(_b) 
matrix xbeta=b[.,2...]   
gen betap=_b[price1] 
matrix score y=xbeta
gen py=normprob(y)
gen wtp= (py/-betap)  
sum py wtp 
drop betap y wtp py
matrix drop _all

log close



